#! /bin/bash
#Author:FanRuikang
#Email:1525297411@qq.com
#Time:2021-09-05 18:56:33
#Name:giteeMirror.sh
#Version:V1.0
#Description: download your gitee repos

PAGES=5 # Default number of pages in gitee repos.
if [ -n "$1" ]; then # Test whether command-line argument is present (non-empty).
  username=$1
else
  echo "Usage: bash $(basename "$0") gitee-username total-pages-of-your-repos"
  echo "Example: bash $(basename "$0") foo 5"
  exit 1;
fi
if [ -n "$2" ]; then
  pages=$2
else
  pages=$PAGES # Default, if not specified on command-line.
fi

: >/tmp/my-gitee-repos.txt
for ((page = 1; page <= pages; page++)); do # Double parentheses, and naked "LIMIT"
  curl -s "https://gitee.com/$username/dashboard/projects?page=$page&sort=name" | grep /"$username"/ | grep repo_ | cut -d '>' -f 6 | cut -d '<' -f 1 >>/tmp/my-gitee-repos.txt
done
sort /tmp/my-gitee-repos.txt | uniq | while read -r line; do git clone https://gitee.com/"$username"/"$line".git; done
rm /tmp/my-gitee-repos.txt;
exit 0
